﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace TCC.Framework
{
    public class LanguageTable : DataEditor
    {
        public static new void  OpenWin(DataTable data)
        {
            win = GetWindow<LanguageTable>();
            win.titleContent.text = "本地化语言配置";
            win.minSize = new Vector2(800, 600);
            win.maxSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
            InitDataInfo(data);
            win.Show();
            IsOpen = true;
        }

        public override void EditorField()
        {
            if (_TableInfo._fields != null && _TableInfo._fields.Count > 0)
            {
                if (GUI.Button(new Rect(150, 0, 60, 20), "编辑字段", EditorStyles.toolbarButton))
                {
                    EditorLanguageField.ShowWin(_TableInfo._fields);
                }
            }
        }
    }

    public class EditorLanguageField:AddFieldWin
    {
        public static new void ShowWin(List<Field> fields)
        {
            win = GetWindow<EditorLanguageField>();
            win.titleContent.text = "编辑字段";
            win.minSize = new Vector2(400, 600);
            Init();
            win.Show();
            IsOpen = true;
        }

        public override void WriteFlied()
        {
            base.WriteFlied();
            AddEnum();
        }

        public void AddEnum()
        {
            string path = AssetPathTool.RelativePathToAbsPath(DataEditor._TableInfo._EntityPath);
            string textAsset = File.ReadAllText(path);
            int index = textAsset.IndexOf("enum Languagel");
            if (index > 0)
            {
                int pos = textAsset.IndexOf('{', index);
                int f_bracket = 0;
                int f = 0;
                int h = 0;
                for (int i = index; i < textAsset.Length; i++)
                {
                    if (textAsset[i] == '{')
                        f++;
                    else if (textAsset[i] == '}')
                        h++;
                    if (f != 0 && h != 0 && f == h)
                    {
                        f_bracket = i;
                        break;
                    }
                }
                textAsset = textAsset.Remove(pos + 1, (f_bracket - pos) - 1);
                StringBuilder str = new StringBuilder();
                foreach (var item in DataEditor._TableInfo._fields)
                {
                    if (item._fieldType == FieldType.String)
                        str.Append($"\n\t\t/// <summary>\n\t\t///{item._nickName}\n\t\t/// </summary>\n\t\t{item._fieldName},");
                }
                str.Append("\n\t");
                textAsset = textAsset.Insert(pos + 1, str.ToString());
                File.WriteAllText(path, textAsset);
            }
        }
    }

    [CustomEditor(typeof(LanguageDate), true)]
    public sealed class SetLanguageTableInspector : Editor
    {
        [OnOpenAsset]
        public static bool OnOpenTable(int instanceID, int line)
        {
            LanguageDate data = EditorUtility.InstanceIDToObject(instanceID) as LanguageDate;
            if (data != null)
            {
                LanguageTable.OpenWin(data);
                return true;
            }
            return false;
        }
        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
        }
    }
}
